package com.xxxy.ljh.myapplication.Dao.ClubCreate.Impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.xxxy.ljh.myapplication.Dao.ClubCreate.ClubCreate;
import com.xxxy.ljh.myapplication.dphelper.sqliteDBHelper;
import com.xxxy.ljh.myapplication.model.Association;

public class ClubCreateImpl implements ClubCreate {
    private Context mContext;
    private SQLiteDatabase mDatabase;

    public ClubCreateImpl(Context context) {
        mContext = context;
        mDatabase = new sqliteDBHelper(mContext).getWritableDatabase();
    }

    @Override
    public boolean createClub(Association association) {
        ContentValues values = new ContentValues();
        values.put("association_name", association.getName());
        values.put("founder_id", association.getFounderId());
        values.put("category", association.getCategory());
        values.put("description", association.getDescription());
        values.put("logo", association.getLogo());
        values.put("contact_phone", association.getContactPhone());
        values.put("contact_email", association.getContactEmail());
        // 默认设置为待审核状态
        values.put("audit_status", 0);
        values.put("status", association.getStatus());
        values.put("member_count", association.getMemberCount());
        values.put("create_time", System.currentTimeMillis()/1000+"");
        values.put("update_time", System.currentTimeMillis()/1000+"");

        try {
            long result = mDatabase.insert("association", null, values);
            return result != -1;
        } catch (SQLiteConstraintException e) {
            if (e.getMessage().contains("association_name")) {
                Toast.makeText(mContext, "社团名称已存在，请更换其他名称", Toast.LENGTH_SHORT).show();
            }
        }
        return false;
    }

    public boolean isClubNameExists(String clubName) {
        String[] projection = {"association_name"}; // 只查询association_name列
        String selection = "association_name = ?";
        String[] selectionArgs = {clubName};
        Cursor cursor = mDatabase.query("association", projection, selection, selectionArgs, null, null, null);
        boolean exists = cursor.getCount() > 0;
        cursor.close();
        return exists;
    }
}
